<?php

namespace App\Services\ApplyGoldRecordUploadVoucher;

use Closure;
use App\Models\User;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\Api\V1\Requests\FormRequest;

class ApplyGoldRecordUploadVoucherRequest extends FormRequest
{
	
	public function rules(): array
	{
		return [
			'record' => ['string', $this->record()],
			'voucher' => ['required']
		];
	}

	public function record()
	{
		if(!request('record')){
			return fail('请提供正确的抢购记录');

		}
		return function ($key, $value, $fail) {

			$user = $this->user();
			if (! $user->isRecordInUnpaidCache($value)) {
				return fail('请提供正确的抢购记录');
			} elseif (! Redis::exists($value)) {
				return fail("目标记录不存在:{$value}");
			} elseif (Redis::hGet($value, 'is_paid')) {
				return fail("目标记录已付款:{$value}");
			/*} elseif ( Redis::hGet($value, 'voucher')) {
				return fail("目标记录已上传凭证:{$value}");*/
			} elseif ( Redis::hGet($value, 'is_examine')) {
				return fail("目标记录已审核:{$value}");
			} elseif ( !Redis::hGet($value, 'resell_user_id')) {
				return fail("目标记录不为用户寄卖商品");
			} 

			return true;
		};
	}

	
}
